Component({
    properties: {
        propName: { // 属性名
            type: String, // 类型（必填），目前接受的类型包括：String, Number, Boolean, Object, Array, null（表示任意类型）
            value: 'val', // 属性初始值（必填）
            observer: function(newVal, oldVal) {
                // 属性被改变时执行的函数（可选）
            }
        }
    },

    data: { // 私有数据，可用于模板渲染
        value_ymd:[0, 0, 0],
        output_ymd:{},
        status_ymd:false,
        text_ymd:'',
        value_hms:[0,0,0],
        output_hms:{},
        status_hms:false,
        text_hms:'',
        time:{
            stamp:'',
            year:'',
            month:'',
            day:'',
            hour:'',
            minute:'',
            second:''
        },
        year:[],
        month:[],
        day:[],
        hour:[],
        minute:[],
        second:[]
    }, 

    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () {
    },

    detached: function () {},

    methods: {
        settimedata:function(){
            var arr = Object.assign(this.data.output_ymd,this.data.output_hms)
            // console.log(arr)
            this.triggerEvent('settimedata',arr)
        },
        init_ymd:function(){
            const that = this;
            var time = {}
            time.stamp = parseInt(new Date().getTime() / 1000);
            time.year = new Date().getFullYear();
            time.month = new Date().getMonth() + 1;
            time.day = new Date().getDate();
            time.hour = new Date().getHours();
            time.minute = new Date().getMinutes();
            time.second = new Date().getFullYear();
            this.setData({
                time,
                status_ymd:true
            },function(){
                if(that.data.text_ymd ==''){
                    that.output_int_ymd(that.data.value_ymd,that.get_year(),that.get_month(),that.get_day(that.data.value_ymd[0],that.data.value_ymd[1]))
                }else{
                    that.output_ymd(that.data.value_ymd,that.get_year(),that.get_month(),that.get_day(that.data.value_ymd[0],that.data.value_ymd[1]))
                }
            })
        },
        init_hms:function(){
            const that = this;
            var time = {}
            time.stamp = parseInt(new Date().getTime() / 1000);
            time.year = new Date().getFullYear();
            time.month = new Date().getMonth() + 1;
            time.day = new Date().getDate();
            time.hour = new Date().getHours();
            time.minute = new Date().getMinutes();
            time.second = new Date().getSeconds();
            this.setData({
                time,
                status_hms:true
            },function(){
                if(that.data.text_hms == ''){
                    that.output_init_hms(that.data.value_hms,that.get_hour(),that.get_minute(),that.get_second())
                }else{
                    that.output_hms(that.data.value_hms,that.get_hour(),that.get_minute(),that.get_second())
                }
            })
        },
        change_ymd:function(e){
            // console.log(e.detail.value)
            const that = this;
            var value = e.detail.value;
            this.setData({
                value_ymd:[value[0],value[1],value[0]!=this.data.value_ymd[0]?0:(value[1]!=this.data.value_ymd[1]?0:value[2])]
            },function(){
                that.output_ymd(that.data.value_ymd,that.data.year,that.data.month,that.get_day(that.data.value_ymd[0],that.data.value_ymd[1]))
            })
        },
        change_hms:function(e){
            // console.log(e)
            const that = this;
            var value = e.detail.value;
            this.setData({
                value_hms:value
            },function(){
                that.output_hms(that.data.value_hms,that.data.hour,that.data.minute,that.get_second())
            })
        },
        output_ymd:function(value,year_data,month_data,day_data){
            const that = this;
            var output_ymd = {};
            output_ymd.year = year_data[value[0]]
            output_ymd.month = month_data[value[1]]
            output_ymd.day = day_data[value[2]]
            // console.log(output_ymd)
            this.setData({
                output_ymd,
            })
        },
        output_int_ymd:function(value,year_data,month_data,day_data){
            const that = this;
            var value_now = [0,0,0]
            //设置默认时间（当前时间）
            for(var y of year_data){
                if(y.value == this.data.time.year){
                    break;
                }else{
                    value_now[0]++
                }
            }
            for(var y of month_data){
                if(y.value == this.data.time.month){
                    break;
                }else{
                    value_now[1]++
                }
            }
            for(var y of day_data){
                if(y.value == this.data.time.day){
                    break;
                }else{
                    value_now[2]++
                }
            }
            // console.log(value_now)
            var output_ymd = {};
            output_ymd.year = year_data[value_now[0]]
            output_ymd.month = month_data[value_now[1]]
            output_ymd.day = day_data[value_now[2]]
            // console.log(output_ymd)
            this.setData({
                output_ymd,
                value_ymd:value_now
            })
        },
        output_hms:function(value,hour_data,minute_data,second_data){
            const that = this;
            // console.log(value,hour_data,minute_data,second_data)
            var output_hms = {};
            output_hms.hour = hour_data[value[0]]
            output_hms.minute = minute_data[value[1]]
            output_hms.second = second_data[value[2]]
            // console.log(output_hms)
            this.setData({
                output_hms
            })
        },
        output_init_hms:function(value,hour_data,minute_data,second_data){
            const that = this;
            var value_now = [0,0,0]
            //设置默认时间（当前时间）
            for(var y of hour_data){
                if(y.value == this.data.time.hour){
                    break;
                }else{
                    value_now[0]++
                }
            }
            for(var y of minute_data){
                if(y.value == this.data.time.minute){
                    break;
                }else{
                    value_now[1]++
                }
            }
            for(var y of second_data){
                if(y.value == this.data.time.second){
                    break;
                }else{
                    value_now[2]++
                }
            }
            console.log(value_now)
            var output_hms = {};
            output_hms.hour = hour_data[value_now[0]]
            output_hms.minute = minute_data[value_now[1]]
            output_hms.second = second_data[value_now[2]]
            this.setData({
                output_hms,
                value_hms:value_now
            })
        },
        sure_ymd:function(){
            const that = this;
            // console.log(this.data.output_ymd.year.name+this.data.output_ymd.month.name+this.data.output_ymd.day.name)
            this.setData({
                text_ymd:this.data.output_ymd.year.name+this.data.output_ymd.month.name+this.data.output_ymd.day.name
            },function(){
                that.close_ymd()
                that.settimedata()
            })
        },
        sure_hms:function(){
            const that = this;
            console.log(this.data.output_hms)
            this.setData({
                text_hms:this.data.output_hms.hour.name+this.data.output_hms.minute.name+this.data.output_hms.second.name
            },function(){
                that.close_hms()
                that.settimedata()
            })
        },
        close_ymd:function(){
            this.setData({
                status_ymd:false
            })
        },
        close_hms:function(){
            this.setData({
                status_hms:false
            })
        },
        get_year:function(){
            var now_year = this.data.time.year
            var year = [];
            for(var i = now_year-20;i<=now_year+20;i++){
                var obj = {};
                obj.name = i+'年';
                obj.value = i;
                year.push(obj)
            }
            this.setData({
                year
            })
            return year;
        },
        get_month:function(){
            var month = [];
            for(var i = 1;i<=12;i++){
                var obj = {};
                obj.name = i+'月';
                obj.value = i;
                month.push(obj)
            }
            this.setData({
                month
            })
            return month;
        },
        get_day:function(val_year,val_month){
            var day = [];
            var year = this.data.year[val_year].value;
            var month = this.data.month[val_month].value;
            // console.log(year,month)
            if(year%4==0&&year%100!=0){  //判断闰年
                if(month!=2){
                    if(month%2!=0){
                        for(var i = 1;i<=31;i++){
                            var obj = {};
                            obj.name = i+'日';
                            obj.value = i;
                            day.push(obj)
                        }
                    }else{
                        for(var i = 1;i<=30;i++){
                            var obj = {};
                            obj.name = i+'日';
                            obj.value = i;
                            day.push(obj)
                        }
                    }
                }else{
                    for(var i = 1;i<=29;i++){
                        var obj = {};
                        obj.name = i+'日';
                        obj.value = i;
                        day.push(obj)
                    }
                }
            }else{
                if(month!=2){
                    if(month%2!=0){
                        for(var i = 1;i<=31;i++){
                            var obj = {};
                            obj.name = i+'日';
                            obj.value = i;
                            day.push(obj)
                        }
                    }else{
                        for(var i = 1;i<=30;i++){
                            var obj = {};
                            obj.name = i+'日';
                            obj.value = i;
                            day.push(obj)
                        }
                    }
                }else{
                    for(var i = 1;i<=28;i++){
                        var obj = {};
                        obj.name = i+'日';
                        obj.value = i;
                        day.push(obj)
                    }
                }
            }
            this.setData({
                day
            })
            return day;
        },
        get_hour:function(){
            var hour = []
            for(var i = 1;i<=24;i++){
                var obj = {};
                obj.name = i+'时';
                obj.value = i;
                hour.push(obj)
            }
            this.setData({
                hour
            })
            return hour;
        },
        get_minute:function(){
            var minute = []
            for(var i = 1;i<=60;i++){
                var obj = {};
                obj.name = i+'分';
                obj.value = i;
                minute.push(obj)
            }
            this.setData({
                minute
            })
            return minute;
        },
        get_second:function(){
            var second = []
            for(var i = 1;i<=60;i++){
                var obj = {};
                obj.name = i+'秒';
                obj.value = i;
                second.push(obj)
            }
            this.setData({
                second
            })
            return second;
        },
    }
});